package de.mmis.core.infrastructure.gui;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Map.Entry;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import de.mmis.core.base.abstracttree.Tree;
import de.mmis.core.base.infrastructure.PublishedDeviceDescription;

/**
 * show all information contained in a {@link PublishedDeviceDescription}
 * 
 * @author Martin Nyolt
 * 
 */
public class DetailWindow extends JFrame {

	private static final long serialVersionUID = -5141776988428722737L;

	private class TableModel extends AbstractTableModel {

		private static final long serialVersionUID = -2592836355638150679L;

		private final String[] columnNames = new String[] { "Key", "Value" };

		private final Object[] entries = descr.getProperties().entrySet()
				.toArray();

		/**
		 * @return the appropriate name
		 */
		@Override
		public String getColumnName(int column) {
			if (column < 0 || column >= getColumnCount())
				return "";

			return columnNames[column];
		}

		@Override
		public int getRowCount() {
			return descr.getProperties().size();
		}

		@Override
		public int getColumnCount() {
			return 2;
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			@SuppressWarnings("unchecked")
			Entry<Tree, Tree> e = (Entry<Tree, Tree>) entries[rowIndex];
			switch (columnIndex) {
			case 0:
				return e.getKey();
			default:
				return e.getValue();
			}
		}
	}

	private final PublishedDeviceDescription descr;

	private JTextField text_id;
	private JTextField text_deviceHost;
	private JTextField text_type;
	private JTextField text_host;
	private JTextField text_port;
	private JTable table_info;
	private JScrollPane scroll_info;

	public DetailWindow(PublishedDeviceDescription descr) {
		super(descr.getID().toString());
		this.descr = descr;
		initGUI();
	}

	private void initGUI() {
		GridBagLayout layout = new GridBagLayout();
		layout.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
		layout.rowHeights = new int[] { 1, 1, 1, 1, 1, 1 };
		layout.columnWeights = new double[] { 0.0, 1.0 };
		layout.columnWidths = new int[] { 1, 1 };

		setLayout(layout);
		Insets insets = new Insets(5, 5, 5, 5);

		{
			text_id = new JTextField(descr.getID().toString());
			text_id.setEditable(false);

			add(new JLabel("ID: "), new GridBagConstraints(0, 0, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
			add(text_id, new GridBagConstraints(1, 0, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
		}
		{
			text_deviceHost = new JTextField(descr.getDeviceHost());
			text_deviceHost.setEditable(false);

			add(new JLabel("DeviceHost: "), new GridBagConstraints(0, 1, 1, 1,
					0, 0, GridBagConstraints.CENTER,
					GridBagConstraints.HORIZONTAL, insets, 0, 0));
			add(text_deviceHost, new GridBagConstraints(1, 1, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
		}
		{
			text_type = new JTextField(descr.getID().toString());
			text_type.setEditable(false);

			add(new JLabel("Type: "), new GridBagConstraints(0, 2, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
			add(text_type, new GridBagConstraints(1, 2, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
		}
		{
			text_host = new JTextField(descr.getHostIP());
			text_host.setEditable(false);

			add(new JLabel("Host: "), new GridBagConstraints(0, 3, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
			add(text_host, new GridBagConstraints(1, 3, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
		}
		{
			text_port = new JTextField(descr.getPort() + "");
			text_port.setEditable(false);

			add(new JLabel("Port: "), new GridBagConstraints(0, 4, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
			add(text_port, new GridBagConstraints(1, 4, 1, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
					insets, 0, 0));
		}
		{
			table_info = new JTable(new TableModel());
			scroll_info = new JScrollPane(table_info);
			add(scroll_info, new GridBagConstraints(0, 5, 2, 1, 0, 0,
					GridBagConstraints.CENTER, GridBagConstraints.BOTH, insets,
					0, 0));
		}

		pack();
		setSize(450, 350);
	}
}
